<?php

$graphStyle = array(
    'splines' => 'true',
    'bgcolor' => 'white',
);

$edgeStyle = array(
    'color' => 'darkgray',
    'arrowsize' => '2',
);

$nodeStyle = array(
    'fontname' => 'Verdana',
    'style' => 'filled',
    'color' => 'grey',
);
if (isset($options['layout'])) {
    $graphStyle['layout'] = $options['layout'];
}
?>

digraph G {
	graph [<?php $i = 0; foreach($graphStyle as $k=>$v) { if($i>0) { echo ','; } ; echo $k . '='.$v; $i++; } ?>];
	edge [<?php $i = 0; foreach($edgeStyle as $k=>$v) { if($i>0) { echo ','; } ; echo $k . '='.$v; $i++; } ?>];
	node [<?php $i = 0; foreach($nodeStyle as $k=>$v) { if($i>0) { echo ','; } ; echo $k . '='.$v; $i++; } ?>];

<?php

foreach($data as $node=>$items) {
    $count = count($items);
    if (true === isset($options['min'])) {
        if ($options['min']>$count) {
            continue;
        }
    }
    if (true === isset($options['max'])) {
        if ($options['max']<$count) {
            continue;
        }
    }
    if (true === isset($options['n'])) {
        if ($options['n']!=$count) {
            continue;
        }
    }
    if (true === isset($options['none'])) {
        if (0 !== $count) {
            continue;
        }
    }
    if (true === isset($options['node'])) {
        if ($node !== $options['node']) {
            continue;
        }
    }
    if (true === isset($options['class'])) {
        if ($node !== $options['class']) {
            continue;
        }
    }
    if (true === isset($options['package'])) {
        if ($options['package'].'_' !== substr($node,0,strlen($options['package'])+1)) {
            continue;
        }
    }
    if (true === isset($options['dir'])) {
        if ($options['dir'] !== substr($node,0,strlen($options['dir']))) {
            continue;
        }
    }
    if (true === isset($options['prefix'])) {
        if ($options['prefix'] !== substr($node,0,strlen($options['prefix']))) {
            continue;
        }
    }
    switch($count) {
        case 0:
            $color = 'white';
            break;
        case 1:
            $color = 'lightgrey';
            break;
        case 2:
            $color = 'lightyellow';
            break;
        case 3:
            $color = 'yellow';
            break;
        case 4:
            $color = 'orange';
            break;
        case 5:
            $color = 'orangered';
            break;
        case 6:
            $color = 'crimson';
            break;
        case 7:
            $color = 'red';
            break;
        case $count > 7:
            $color = 'brown';
            break;
    }

    ?>
	"<?php echo utf8_encode($node) ?>" [fillcolor="<?php echo $color ?>"];
<?php
    foreach($items as $subNode=>$weight) {
        if (false === array_key_exists($subNode,$data)) {
?>
	"<?php echo utf8_encode($subNode) ?>" [fillcolor="blue"];
<?php }
?>
	"<?php echo utf8_encode($node) ?>" -> "<?php echo $subNode?>" [label=<?php echo $weight?>, size=<?php echo $weight?>];
<?php
    }
}
?>
}